<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import * as echarts from 'echarts'

const router = useRouter()
const loading = ref(false)
const classes = ref([])
const analysisData = ref({}) // 存储每个班级的分析数据

// 获取所有班级
const fetchClasses = async () => {
  loading.value = true
  try {
    const res = await axios.get('/api/admin/class-name/list')
    if (res.data && res.data.code === '200') {
      classes.value = res.data.data
      // 获取每个班级的分析数据
      for (const cls of classes.value) {
        await fetchClassAnalysis(cls.id)
      }
    }
  } catch (error) {
    console.error('获取班级列表失败:', error)
  }
  loading.value = false
}

// 获取单个班级的分析数据
const fetchClassAnalysis = async (classId) => {
  try {
    const res = await axios.get('/api/admin/class-analysis/' + classId + '/summary')
    if (res.data && res.data.code === '200') {
      analysisData.value[classId] = res.data.data
    }
  } catch (error) {
    console.error('获取班级分析数据失败:', error)
  }
}

// 跳转到班级详情页
const goToDetail = (classId) => {
  router.push('/tabus/class-analysis/' + classId)
}

// 初始化小型图表
const initMiniChart = (el, data) => {
  const chart = echarts.init(el)
  const option = {
    tooltip: {
      trigger: 'axis'
    },
    grid: {
      top: 5,
      right: 5,
      bottom: 5,
      left: 5,
      containLabel: false
    },
    xAxis: {
      type: 'category',
      show: false,
      data: data.dates
    },
    yAxis: {
      type: 'value',
      show: false
    },
    series: [
      {
        data: data.values,
        type: 'line',
        smooth: true,
        showSymbol: false,
        lineStyle: {
          color: '#409EFF'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(64,158,255,0.3)' },
            { offset: 1, color: 'rgba(64,158,255,0.1)' }
          ])
        }
      }
    ]
  }
  chart.setOption(option)
}

onMounted(() => {
  fetchClasses()
})
</script>

<template>
  <div class="class-analysis">
    <div class="page-header">
      <h2>班级分析</h2>
      <p>查看各个班级的学习情况和课堂表现分析</p>
    </div>
    
    <el-row :gutter="20" v-loading="loading">
      <el-col :span="8" v-for="cls in classes" :key="cls.id" class="mb-4">
        <el-card class="class-card" @click="goToDetail(cls.id)">
          <template #header>
            <div class="card-header">
              <h3>{{ cls.name }}</h3>
              <el-tag size="small">{{ analysisData[cls.id]?.totalCourses || 0 }} 门课程</el-tag>
            </div>
          </template>
          
          <div class="card-content">
            <div class="metrics">
              <div class="metric-item">
                <div class="metric-label">平均抬头率</div>
                <div class="metric-value">{{ analysisData[cls.id]?.avgRaiseRate?.toFixed(1) || 0 }}%</div>
              </div>
              <div class="metric-item">
                <div class="metric-label">平均参与度</div>
                <div class="metric-value">{{ analysisData[cls.id]?.avgParticipation?.toFixed(1) || 0 }}%</div>
              </div>
            </div>
            
            <div class="chart-container" ref="chartRefs">
              <!-- 这里会通过 initMiniChart 初始化趋势图 -->
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped>
.class-analysis {
  padding: 20px;
  background: #f5f7fa;
  min-height: 100vh;
}

.page-header {
  margin-bottom: 24px;
}

.page-header h2 {
  margin: 0;
  font-size: 24px;
  color: #000;
}

.page-header p {
  margin: 8px 0 0;
  color: #666;
}

.class-card {
  cursor: pointer;
  transition: all 0.3s;
}

.class-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 {
  margin: 0;
  font-size: 18px;
  color: #000;
}

.metrics {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.metric-item {
  text-align: center;
}

.metric-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 4px;
}

.metric-value {
  font-size: 24px;
  color: #000;
  font-weight: 600;
}

.chart-container {
  height: 100px;
  margin-top: 10px;
}

.mb-4 {
  margin-bottom: 16px;
}
</style> 